Title Banner

Previous Book Contents Book Index Next

Inside Macintosh: QuickDraw GX Printing /
Chapter 2 - Core Printing Features / Using Core Printing Features


Retrieving a Job Object When Opening a Document

When the user chooses the Open menu command from the File menu to open a document, you need to retrieve its job object. To retrieve a job object, you unflatten it using one of the QuickDraw GX unflattening functions. For an introduction to flattening and unflattening QuickDraw GX print objects, see the chapter "Introduction to Printing With QuickDraw GX" in this book.

There are two methods to retrieving a job object depending on how you have previously saved it. If you saved the job object using the GXFlattenJobToHdl function, you should retrieve it using the GXUnflattenJobFromHdl function. If you saved the job object using the GXFlattenJob function, you should retrieve it using the GXUnflattenJob function. For details on the GXFlattenJobToHdl and GXFlattenJob functions, see "Saving a Job Object With a Document File," which begins on page 2-24.

Retrieving a Job Object From a Handle

This section describes how to use the GXUnflattenJobFromHdl function to retrieve a job object and its related data.

When a user chooses the Open menu command from the File menu, you should open the document and retrieve its previously saved job object. To do so, you open the document's data fork and resource fork. The MyOpenDocument function in Listing 2-10 accomplishes this.

If there are no errors, you should specify the document's file system specification information, its title, and its window's title. If there is a job object resource saved in the resource file, you should load it and unflatten it using the GXUnflattenJobFromHdl function.

After the job object is unflattened, you can load the data for the document's pages. Finally, you should close the document's data fork and resource fork. Listing 2-10 shows how to open a document and retrieve its job object using the GXUnflattenJobFromHdl function.

Listing 2-10 Using the GXUnflattenJobFromHdl function to retrieve a job object

OSErr MyOpenDocument(MyDocumentPtr myDocument)
{
   OSErr                err;
   Handle               theJobData;
   short                oldResFile;
   short                dataRefNum = -1, resRefNum = -1;
   StandardFileReply    sfReply;
   SFTypeList           myTypeList;

   /* Let the user select a document to open. */
   oldResFile = CurResFile();
   
   myTypeList[0] = kMyDocType;
   StandardGetFile(nil, 1, &myTypeList, &sfReply);
   if (!sfReply.sfGood)
      return noErr;

   /* Open the selected file's data fork and resource fork. */
   err = FSpOpenDF(&sfReply.sfFile, fsRdWrPerm, &dataRefNum);
   if (err == noErr)
   {
      resRefNum = HOpenResFile(sfReply.sfFile.vRefNum, 
                                 sfReply.sfFile.parID,
                                 sfReply.sfFile.name, fsRdPerm);
      err = ResError();
   }
   if (err) return err;

   /* 
      If no error, set the document's file system specification
      information, its title, and its window's title.
   */
   BlockMove(&sfReply.sfFile, &myDocument->documentFSSpec, 
               sizeof(FSSpec));
   BlockMove(&sfReply.sfFile.name, myDocument->documentTitle,
            (long) sfReply.sfFile.name[0] +1);
   SetWTitle(myDocument->documentWindow, 
               myDocument->documentTitle);

   /* 
      If there's a job object resource saved, 
      load and unflatten it.
   */
   UseResFile(resRefNum);
   theJobData = Get1Resource(kMyJobType, kMyJobID);
   if (theJobData != nil)
   {
      GXUnflattenJobFromHdl(myDocument->documentJob, theJobData);
      err = GXGetJobError(myDocument->documentJob);
      ReleaseResource(theJobData);
   }

   /* 
      Place your application-specific code here to load 
      other data associated with the document's pages.
   */
   ...

   /* Close the data fork and resource fork of this document. */
   if (dataRefNum != -1) FSClose(dataRefNum);
   if (resRefNum  != -1) CloseResFile(resRefNum);
   UseResFile(oldResFile);
   return err;
}

Retrieving a Job Object Using an Unflattening Function

This section describes how to use the GXUnflattenJob function to retrieve a job object. You specify a pointer to an unflattening function in the aPrintingFlattenProc parameter of the GXUnflattenJob function.

An example of an unflattening function named MyUnflattenFunction that you could write is as follows:

OSErr MyUnflattenJobFunc(long dataSize, void *data,
                           void *dataRefNum)
{
   long  count = dataSize;
   return FSRead((short) dataRefNum, &count, data);
}
QuickDraw GX calls your unflattening function multiple times as it retrieves job object-related data from disk. The dataSize parameter specifies the number of bytes for this segment of the job object data. The data parameter specifies a pointer to the segments of job object data to read. The dataRefNum parameter specifies the file reference number of the open file from which you want to read.

Listing 2-11 shows how to retrieve a job object using the GXUnflattenJob function.

Listing 2-11 Using the GXUnflattenJob function to retrieve a job object

OSErr MyLoadJobFromDataFork(MyDocumentPtr myDocument, 
                              short dataRefNum)
{
   OSErr    err;

/* 
   Reset the file's position to the beginning of the data fork, 
   read and then unflatten the job object from there.
*/
   err = SetFPos(dataRefNum, fsFromStart, 0);
   if (err == noErr)
   {
      GXUnflattenJob(myDocument->documentJob,
                     (gxPrintingFlattenProc) MyUnflattenJobFunc,
                     (void *) dataRefNum);
      err = GXGetJobError(myDocument->documentJob);
   }
   return err;
}

Previous Book Contents Book Index Next

© Apple Computer, Inc.
7 JUL 1996




Navigation graphic, see text links

Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help